import os
import textwrap
import time

import google.generativeai as genai
import PIL.Image

from IPython.display import Markdown

genai.configure(api_key = 'AIzaSyCYKdkqTOdlmjdauzFVDoFz_rEYwsazTH0')

def to_markdown(text):
  text = text.replace('•', '  *')
  return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

model = genai.GenerativeModel('gemini-pro-vision')

files = os.listdir("/Users/yuwang/Desktop/Gemini in Political Science/data/images")

def image_analysis(image_name):
    print("Image name", image_name)
    img = PIL.Image.open("./../data/images/" + image_name)
    start = time.time()
    prompt = 'Generate a brief caption for the given image.'
    try:
    	response = model.generate_content([prompt, img])
    	print(response.text.strip())
    except:
        print("failed at", image_name)
    #print("====" * 10)


x = """abc-news_0019_8.jpg
vice-news_0000_1.jpeg
buzzfeed_0000_2.png
abc-news_0005_4.jpg"""
for i in x.split("\n"):
	image_analysis(i.strip())
